import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/painting.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:tot_android_flutter/base/BasePage.dart';
import 'package:tot_android_flutter/common/widget/app_bar.dart';
import 'package:tot_android_flutter/pages/login/login_controller.dart';
import 'package:tot_android_flutter/themes/color_palettes.dart';
import 'package:tot_android_flutter/utils/utils_ext.dart';

import '../../themes/color_palettes.dart';

class LoginPage extends BasePage<LoginController> {
  LoginPage({super.key});

  @override
  AppBar? buildAppBar() {
    return commonAppBar(
        backgroundColor: ColorPalettes.instance.primary,
        statusBarColor: ColorPalettes.instance.firstText,
        bottom: commonTitleBar(
            title: "登录",
            leftClick: () {
              Get.back();
            }));
  }

  @override
  Widget buildBody(BuildContext context) {
    return _buildContent(context);
  }

  Widget _buildContent(BuildContext context) {
    // return widget_name_input(context);
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          const SizedBox(
            height: 16,
          ),
          widget_name_input(context),
          const SizedBox(
            height: 16,
          ),
          widget_pw_input(context),
          const SizedBox(
            height: 16,
          ),
          widget_tip_text(),
          const SizedBox(
            height: 16,
          ),
          login_button(context),
        ],
      ),
    );
  }

  Widget widget_name_input(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 8.w),
      decoration: BoxDecoration(
        border: Border.all(
          color: controller.hasNameFocus.value
              ? ColorPalettes.instance.primary
              : Colors.black, // 边框颜色
          width: 2.0, // 边框宽度
        ),
        borderRadius: BorderRadius.circular(8.0), // 可选：边框圆角
      ),
      child: Row(
        children: [
          Text("用户名："),
          SizedBox(width: 16.w),
          Expanded(
            child: TextField(
              cursorColor: ColorPalettes.instance.primary,
              focusNode: controller.focusNameNode,
              controller: controller.nameTextController,
              decoration:
                  InputDecoration(hintText: "请输入用户名", border: InputBorder.none),
            ),
          ),
          GestureDetector(
              onTap: () {
                controller.nameTextController.text = "";
              },
              child: !controller.nameIsEmpty.value
                  ? Icon(
                      TDIcons.close_circle_filled,
                      color: TDTheme.of(context).fontGyColor3,
                    )
                  : Container())
        ],
      ),
    );
  }

  Widget widget_pw_input(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 8.w),
      decoration: BoxDecoration(
        border: Border.all(
          color: controller.hasPwFocus.value
              ? ColorPalettes.instance.primary
              : Colors.black, // 边框颜色
          width: 2.0, // 边框宽度
        ),
        borderRadius: BorderRadius.circular(8.0), // 可选：边框圆角
      ),
      child: Row(
        children: [
          Text("密码："),
          SizedBox(width: 16.w),
          Expanded(
            child: TextField(
              obscureText: controller.browseOn.value,
              cursorColor: ColorPalettes.instance.primary,
              focusNode: controller.focusPwNode,
              controller: controller.passwordTextController,
              decoration:
                  InputDecoration(hintText: "请输入密码", border: InputBorder.none),
            ),
          ),
          GestureDetector(
              onTap: () {
                controller.browseOn.value = !controller.browseOn.value;
              },
              child: controller.browseOn.value
                  ? Icon(
                      TDIcons.browse,
                      color: TDTheme.of(context).fontGyColor3,
                    )
                  : Icon(TDIcons.browse_off,
                      color: TDTheme.of(context).fontGyColor3))
        ],
      ),
    );
  }

  Widget widget_tip_text() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Checkbox(
            value: controller.isChecked.value,
            onChanged: (changeValue) {
              controller.isChecked.value = changeValue ?? false;
            }),
        Text("我已阅读并同意",
            style: TextStyle(
                color: ColorPalettes.instance.firstText, fontSize: 13.w)),
        GestureDetector(
            onTap: () {},
            child: Text("《隐私政策》",
                style: TextStyle(color: ColorPalettes.instance.secondText))),
        Text("和",
            style: TextStyle(
                color: ColorPalettes.instance.firstText, fontSize: 13.w)),
        GestureDetector(
            onTap: () {},
            child: Text("《用户协议》",
                style: TextStyle(color: ColorPalettes.instance.secondText))),
      ],
    );
  }

  Widget login_button(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        // 处理登录逻辑
        _onLoginOrRegister();
      },
      style: ButtonStyle(
        backgroundColor:
            WidgetStateProperty.all<Color>(ColorPalettes.instance.primary),
        shape: WidgetStateProperty.all<RoundedRectangleBorder>(
          RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(20.0), // 设置圆角半径
          ),
        ),
      ),
      child: Text(
        '登录',
        style: TextStyle(
          fontSize: 16.0,
          color: Colors.white,
        ),
      ),
    );
  }

  _onLoginOrRegister() {
    controller.login();
  }
}
